jQuery(function($) {

	streamers = [];
	LIBREANTENE = "";
	IP = "";
	INVITE = "";
	var regex = new RegExp("^(http[s]?:\\/\\/(www\\.)?|ftp:\\/\\/(www\\.)?|www\\.){1}([0-9A-Za-z-\\.@:%_\+~#=]+)+((\\.[a-zA-Z]{2,3})+)(/(.)*)?(\\?(.)*)?");

	loadStreamer();

	function loadStreamer(){
		$.ajax({
			'url' :  './streamers.json'
		}).done(function(d){
			streamers = d;
			for(id in streamers){
				s = streamers[id];
				$('#streamer').append(new Option(s.name, s.name, true, true));
				$("#streamer").val($("#streamer option:first").val());

			}
		});
		$.get('./template/infostreamer-libreantaine.html',{}, function(thtml){
			LIBREANTENE = thtml;
		});
		$.get('./template/info-streamer-invite.html',{}, function(thtml){
			INVITE = thtml;
		});
		$.get('./template/info-streamer-IP.html',{}, function(thtml){
			IP = thtml;
		});
	}

	$('#form-info-streamer').on('submit', function(){

		streamerName = $("#streamer").val();

		$.get('./template/info-streamer.html',{}, function(thtml){

			if($("#starth").val() == ''){
				strh = "En live jusqu'à ";
			}else{
				strh = $("#starth").val() + " - ";
			}
			streamer = getStreamerByName(streamerName);
			thml1 = replaceTemplate(thtml, '{{NAME}}', streamer.name);
			thml2 = replaceTemplate(thml1, '{{TWITTER}}', streamer.twitter);
			thml3 = replaceTemplate(thml2, '{{FACEBOOK}}', streamer.facebook);
			thml4 = replaceTemplate(thml3, '{{YOUTUBE}}', streamer.youtube);
			thml5 = replaceTemplate(thml4, '{{MAIL}}', streamer.mail);
			thml6 = replaceTemplate(thml5, '{{STRH}}', strh);
			thml7 = replaceTemplate(thml6, '{{ENDH}}', $("#endh").val());
			thml8 = replaceTemplate(thml7, '{{GAMETITLE}}',  $("#gamename").val());
			thml10 = replaceTemplate(thml8, '{{URL}}',  $("#gameurl").val());
			thml9 = replaceTemplate(thml10, '{{LIBREANTENE}}', $('#lbant').is(':checked') ? LIBREANTENE : "");
			thtmlIP1 = replaceTemplate(IP, '{{IPIP}}', ($('#ip').val() != "") ? $('#ip').val() : "");
			thtmlIP = replaceTemplate(thtmlIP1, '{{IPURL}}', (regex.test($('#ip').val())) ? $('#ip').val() : "");
			thtml11 = replaceTemplate(thml9, '{{IP}}', ($('#ip').val() != "") ? thtmlIP : "");
			thtmlInv1 = replaceTemplate(INVITE, '{{INVITE_URL}}', ($('#invite_url').val() != "" && $('#invite_name').val() != "" ) ? $('#invite_url').val() : "");
			thtmlInv2 = replaceTemplate(thtmlInv1, '{{INVITE_NAME}}', ($('#invite_name').val() != "" ) ? $('#invite_name').val() : "");
			rend = replaceTemplate(thtml11, '{{INVITE}}', ($('#invite_name').val() != "" ) ? thtmlInv2 : "");
			if(streamer.youtube == null){
				rend = removeElements(rend, "#isyt");
			}
			if(streamer.twitter == null){
				rend = removeElements(rend, "#istw");
				rend = removeElements(rend, "#isyttw");
			}
			if(streamer.facebook == null){
				rend = removeElements(rend, "#isfb");
				rend = removeElements(rend, "#isfbyt");
			}
			if(streamer.mail == null){
				rend = removeElements(rend, "#ismail");
			}
			if($("#gamename").val() == ""){
				rend = removeElements(rend, "#game");
			}
			if($("#endh").val() == ""){
				rend = removeElements(rend, "#time");
			}
			$('#code').val(rend);
			$('#preview').html(rend);
		});
	return false;
	});

	var removeElements = function(text, selector) {
		var wrapped = $("<div>" + text + "</div>");
		wrapped.find(selector).remove();
		return wrapped.html();
	}

	function replaceTemplate(html,old,nw){
		//	console.log('rplace : ' + old + " => " + nw );
		h = html.replace(old, nw);
		return h;	
	}

	function getStreamerByName(name){

		for(id in streamers){
			s = streamers[id];
			if(name === s.name){
				return s;
			}
		}
		return null;
	}
});